# -*- coding: utf-8 -*-
import numpy
no_char_display = 1 #需要打印出点阵字符请设置为1，调试使用
font_data = {}
with open(".\\fotbin",'rb') as font_bin_f:
    tmp_str = "ABCD中文菜单测试12345"
    tmp_str = list(tmp_str)
    tmp_str.sort()
    for str_byte in tmp_str:
        addr_start = 0
        addr_len = 0
        column_num = 0
        if (ord(str_byte) >= 0x20) and (ord(str_byte) <= 0x7e):
            addr_start = (ord(str_byte) - 0x20) * 16 + 0x1db
            addr_len = 16
            column_num = 8
        #c280 -> 1100 0010 1000 0000 -> 80
        #d1bf -> 1101 0001 1011 1111 -> 47f
        if (ord(str_byte) >= 0x80) and (ord(str_byte) <= 0x47f):
            addr_start = (ord(str_byte)-0x80 * 32)+0x7cb
            addr_len = 32
            column_num = 16
        #0xe28080 -> 0x2000
        #0xefbfbf -> 1110 1111 1011 1111 1011 1111 -> ffff
        if (ord(str_byte) >= 0x2000) and (ord(str_byte) <= 0xffff):
            column_num = 16
            addr_start = ((ord(str_byte)-0x2000)*32+0x87cb)
            addr_len = 32
        font_bin_f.seek(addr_start-1, 0)
        data_bytes = font_bin_f.read(addr_len)
        font_data[str_byte] = data_bytes
        if not no_char_display:
            print("%0s: utf-8:0x%s unicode:%#x addr:%#x hex:%s" %
                  (str_byte, str_byte.encode("utf-8").hex(
                      '_', 2),ord(str_byte), addr_start, data_bytes.hex('_', 2)))
            display_buffer = numpy.zeros((16,16), numpy.int8)
            for i in range(0, column_num):
                bin_lst = list(bin(data_bytes[i+column_num]).replace("0b","").rjust(8, '0')) + list(bin(data_bytes[i]).replace("0b","").rjust(8, '0'))
                display_buffer[i,:] =bin_lst
            #print(len(display_buffer))
            display_buffer = display_buffer.transpose()
            #display_buffer = display_buffer.swapaxes(1,0)
            print("   0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7")
            line_idx = 0
            for idx in range(len(display_buffer)-1,-1,-1):
                line_str = str(display_buffer[idx])
                line_str = line_str.replace("'","")
                line_str = line_str.replace("b","")
                line_str = line_str.replace("0"," ")
                line_str = line_str.replace("1","O")
                print("%0d:%s" % ((line_idx%8),line_str))
                line_idx = line_idx+1

with open(".\\font_data.h", mode='w',encoding='utf-8') as font_data_f:
    font_data_f.write("const unsigned char utf_8_font_data[] = {")
    data_bytes_cnt = 0
    unicode_offset_map = []
    for key in font_data.keys():
        line_element_cnt = 0
        data_line = ("//%s\n" % key)
        unicode_offset_map.append((data_bytes_cnt<<16)|ord(key))
        for byte_val in font_data[key]:
            if(data_bytes_cnt==0):
                data_line += ("  0x%02x" % byte_val)
            else:
                data_line += (", 0x%02x" % byte_val)
            line_element_cnt += 1
            if (line_element_cnt % 8) == 0:
                data_line += "\n"
            data_bytes_cnt += 1
        #print(data_line)
        font_data_f.write(data_line)
    font_data_f.write("};\n")
    font_data_f.write("unsigned int unicode_offset_map[] = {\n")
    map_bytes_cnt = 0
    for map_data in unicode_offset_map:
        #print(chr(map_data & 0xfff))
        if map_bytes_cnt==0:
            font_data_f.write("  0x%08x/*%s*/" % (map_data, chr(map_data & 0xffff)))
        else:
            font_data_f.write(", 0x%08x/*%s*/" % (map_data, chr(map_data & 0xffff)))
        map_bytes_cnt +=1
        if (map_bytes_cnt%8) == 0:
            font_data_f.write("\n")
    font_data_f.write("};\n#define MAP_TABLE_LEN %0d\n" % map_bytes_cnt)
    print("%0d Bytes used\n" % (data_bytes_cnt+map_bytes_cnt*4))
